package com.smbms.mapper.impl;

import com.dx.domain.SysUser;
import com.smbms.domain.SmbmsUser;
import com.smbms.mapper.SmbmsUserMapper;
import com.smbms.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.lang.Long;
import java.util.ArrayList;
import java.util.Date;

/**
 * @ClassName SmbmsUserMapperImpl
 * @Description TODO:
 * @Author 12994
 * @Date 2024/10/17 21:32
 * @Version 1.0
 **/
public class SmbmsUserMapperImpl implements SmbmsUserMapper {
    SqlSession sqlSession;

    @Override
    public ArrayList<SmbmsUser> listSmbmsUser() {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SmbmsUserMapper.class).listSmbmsUser();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public SmbmsUser findSmbmsUserById(Long id) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SmbmsUserMapper.class).findSmbmsUserById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public int addSmbmsUser(SmbmsUser smbmsUser) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SmbmsUserMapper.class).addSmbmsUser(smbmsUser);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public int updateSmbmsUser(SmbmsUser smbmsUser) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SmbmsUserMapper.class).updateSmbmsUser(smbmsUser);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public int removeSmbmsUserById(Long id) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SmbmsUserMapper.class).removeSmbmsUserById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public ArrayList<SmbmsUser> listSmbmsUserWithPages(Integer pages, Integer count) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SmbmsUserMapper.class).listSmbmsUserWithPages((pages - 1) * count, count);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    // 测试用例
    public static void main(String[] args) {
        SmbmsUserMapper smbmsUserMapper = new SmbmsUserMapperImpl();
        System.out.println("---查询列表");
        smbmsUserMapper.listSmbmsUser().forEach(System.out::println);

        System.out.println("---根据主键查询");
        System.out.println(smbmsUserMapper.findSmbmsUserById(1L));

        System.out.println("---添加");
        SmbmsUser smbmsUser = new SmbmsUser();
        smbmsUser.setId(null);
        smbmsUser.setUserCode("aa");
        smbmsUser.setUserName("aa_name");
        smbmsUser.setUserPassword("aa_password");
        smbmsUser.setGender(1);
        smbmsUser.setBirthday(new Date());
        smbmsUser.setPhone("13112345678");
        smbmsUser.setAddress("水磨沟区");
        smbmsUser.setUserRole(1);
        smbmsUser.setCreatedBy(1L);
        smbmsUser.setCreationDate(new Date());
        smbmsUser.setModifyBy(1L);
        smbmsUser.setModifyDate(new Date());
        System.out.println(smbmsUserMapper.addSmbmsUser(smbmsUser));

        System.out.println("---更新");
        smbmsUser.setAddress("新的地址");
        smbmsUser.setId(1L);
        System.out.println(smbmsUserMapper.updateSmbmsUser(smbmsUser));

        System.out.println("---根据主键删除");
        System.out.println(smbmsUserMapper.removeSmbmsUserById(2L));

        System.out.println("---分页 2, 2");
        smbmsUserMapper.listSmbmsUserWithPages(2, 2).forEach(System.out::println);
    }
}
